﻿var EmployeesFilter = function(container) {
    var self = this;
    var isInitialized = false;
    var isVisible = false;
    var lastTop = 0;

    var currentFilter = {
        punctDeLucru: null,
        departament: null,
        functie: null
    };

    var isScrolledDisabled = false;

    this.hasAnyCriteriaSelected = function() {
        if (!currentFilter)
            return false;

        return currentFilter.punctDeLucru || currentFilter.departament || currentFilter.functie;
    };

    this.getFilterData = function() { return currentFilter; };

    var Elements = {
        punctDeLucru: container.find('.punctDeLucru .selectedValues'),
        departament: container.find('.departament .selectedValues'),
        functie: container.find('.functie .selectedValues'),
        filterExplanation: null,
        overlay: app.CurrentPage.container.find("#grayOverlay")
    };

    this.setFilterData = function(data) {
        self.setPunctDeLucru(data.punctDeLucru);
        self.setDepartament(data.departament);
        self.setFunctie(data.functie);

        self.updateFilterExplanation();
    };

    this.updateFilterExplanation = function() {
        if (!Elements.filterExplanation) {
            Elements.filterExplanation = app.CurrentPage.container.find('.filterExplanation');
            Elements.filterExplanation.tap(function() {
                self.toggle();
            });
        }

        var count = 0;
        if (currentFilter.tipConcediu)
            count++;

        if (!count)
            Elements.filterExplanation.hide();
        else {
            var text = count == 1 ? translate('Filter_Explanation_OneCriteria') : translate('Filter_Explanation_MoreCriteria').format(count);
            Elements.filterExplanation.show().find('span:first').html(text);
        }
    };

    var initialize = function() {
        /* START punct de lucru */
        Elements.punctDeLucru.click(function(e) {
            if (!app.Pages.SelectarePunctDeLucru.instance) {
                app.Pages.SelectarePunctDeLucru.instance = new SelectarePunctDeLucruPage(app.Pages.SelectarePunctDeLucru.container);
                app.Pages.SelectarePunctDeLucru.instance.initialize();
            }
            app.changePage(app.Pages.SelectarePunctDeLucru);
            app.Pages.SelectarePunctDeLucru.instance.setCallback(function(data) {
                self.setPunctDeLucru(data);
                app.changePage(app.Pages.EmployeesList);
                self.show();
            });
            return Event.stopEvent(e);
        });

        Elements.punctDeLucru.parent().find('.cancel').click(function() { resetPunctDeLucru($(this)); });
        /* END punct de lucru */

        /* START departament */
        Elements.departament.parent().find('.cancel').click(function() { resetDepartament($(this)); });
        /* END departament */

        /* START functie */
        Elements.functie.click(function(e) {
            if (!app.Pages.SelectareFunctie.instance) {
                app.Pages.SelectareFunctie.instance = new SelectareFunctiePage(app.Pages.SelectareFunctie.container);
                app.Pages.SelectareFunctie.instance.initialize();
            }
            app.Pages.SelectareFunctie.instance.setCallback(function(data) {
                self.setFunctie(data);
                app.changePage(app.Pages.EmployeesList);
                self.show();
            });
            app.changePage(app.Pages.SelectareFunctie);
            return Event.stopEvent(e);
        });

        Elements.functie.parent().find('.cancel').click(function() { resetFunctie($(this)); });
        /* END tip functie */

        $(document).bind('touchmove', function(e) {
            if (app.CurrentPage.instance == app.Pages.EmployeesList.instance && isScrolledDisabled)
                return Event.stopEvent(e);
        });

        $(document).bind('touchstart', function() {
            if (app.CurrentPage.instance == app.Pages.EmployeesList.instance && self.isVisible())
                if ($(document).scrollTop() != lastTop)
                    $(document).scrollTop(lastTop);
        });

        if (!Elements.overlay.length) {
            Elements.overlay = app.CurrentPage.container.find("#grayOverlay");
            Elements.overlay.bind('touchstart', function(e) { return Event.stopEvent(e); });
        }

        isInitialized = true;
    };

    var applyFilter = function() {
        self.updateFilterExplanation();
        container.trigger('apply');
        self.hide();
    };

    var departamentSelected = function() {
        if (!currentFilter.punctDeLucru || !currentFilter.punctDeLucru.DepartmentsCount)
            return;
        if (!app.Pages.SelectareDepartament.instance) {
            app.Pages.SelectareDepartament.instance = new SelectareDepartamentPage(app.Pages.SelectareDepartament.container);
            app.Pages.SelectareDepartament.instance.initialize();
        }
        app.changePage(app.Pages.SelectareDepartament);
        app.Pages.SelectareDepartament.instance.refresh(currentFilter.punctDeLucru, function(data) {
            self.setDepartament(data);
            app.changePage(app.Pages.EmployeesList);
            self.show();
        });
    };

    var resetFunctie = function(object) {
        currentFilter.functie = null;
        Elements.functie.html(translate('General_Toate'));
        if (object)
            object.hide();
        self.show();
    };

    var resetPunctDeLucru = function(object) {
        self.setPunctDeLucru(null);
        if (object)
            object.hide();
        self.show();
    };

    var resetDepartament = function(object) {
        self.setDepartament(null);
        if (object)
            object.hide();
        self.show();
    };

    this.setPunctDeLucru = function(pct) {
        if (!currentFilter.punctDeLucru || !pct || currentFilter.punctDeLucru.Id != pct.Id) {
            self.setDepartament(null);
        }

        if (!pct || (pct && pct.DepartmentsCount == 0)) {
            Elements.departament.addClass('disabled');
            Elements.departament.unbind();
        } else {
            Elements.departament.unbind().click(function(e) {
                departamentSelected();
                return Event.stopEvent(e);
            });
            Elements.departament.removeClass('disabled');
        }

        currentFilter.punctDeLucru = pct;
        if (pct) {
            Elements.punctDeLucru.html(pct.Name);
            Elements.punctDeLucru.parent().find('.cancel').show();
        } else {
            Elements.punctDeLucru.html(translate('General_Toate'));
            Elements.punctDeLucru.parent().find('.cancel').hide();
        }
    };

    this.setDepartament = function(dpt) {
        currentFilter.departament = dpt;
        if (dpt) {
            Elements.departament.html(dpt.Name);
            Elements.departament.parent().find('.cancel').show();
        } else {
            Elements.departament.html(translate('General_Toate'));
            Elements.departament.parent().find('.cancel').hide();
        }
    };

    this.hasAnyCriteriaSelected = function() {
        return currentFilter.punctDeLucru || currentFilter.departament || currentFilter.functie;
    };

    this.reset = function() {
        self.setFilterData({});
    };

    this.setFunctie = function(data) {
        currentFilter.functie = data;
        if (data) {
            Elements.functie.html(data.Name);
            Elements.functie.parent().find('.cancel').show();
        } else {
            Elements.functie.html(translate('General_Toate'));
            Elements.functie.parent().find('.cancel').hide();
        }
    };

    this.show = function() {
        if (!isInitialized)
            initialize();

        if (!isVisible)
            lastTop = $(document).scrollTop();
        container.css('top', lastTop + 'px').show();
        Elements.overlay.show().css({
            top: (container.position().top + container.outerHeight()) + 'px'
        });

        isScrolledDisabled = container.position().top + container.outerHeight() - $(document).scrollTop() < $(window).height();
        $(document).trigger('overlayOpened');
        isVisible = true;
    };

    this.hide = function() {
        if (container.is(":visible")) {
            container.hide();
            Elements.overlay.hide();
            isScrolledDisabled = false;
            $(document).trigger('overlayClosed');
            lastTop = 0;
            isVisible = false;
        }
    };

    this.toggle = function() {
        if (container.is(":visible"))
            self.hide();
        else self.show();
    };

    this.isVisible = function() {
        return isVisible;
    };

    this.isAnyDialogVisible = function() {
        return false;
    };

    this.closeDialogs = function() {
    };
};